package com.hopu.v1.service.common.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.hopu.v1.service.admin.system.StaffService;
import com.hopu.v1.service.common.OSSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class OSSServiceImpl implements OSSService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Value("${aliyun.oss.urlPrefix}")
    private String urlPrefix;

    @Autowired
    StaffService staffServic;

    //上传文件
    @Override
    public String uploadFile(MultipartFile file) {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 获取文件输入流
            InputStream inputStream = file.getInputStream();
            // 获取文件名
            String fileName = file.getOriginalFilename();
            //截取文件格式
            final String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
            //重新生成文件名
            String name = UUID.randomUUID() + "." + fileType;
            // 上传文件到指定的存储桶
            ossClient.putObject(bucketName, name, inputStream);
            // 关闭文件流
            inputStream.close();
            // 返回文件的URL地址
            return urlPrefix + name;
        } catch (Exception e) {
            return null;
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

    // 下载文件方法
    @Override
    public Map<String, Object> downloadFile(String fileName) throws Exception {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 从OSS获取文件流
            InputStream inputStream = ossClient.getObject(bucketName, fileName).getObjectContent();

            // 将文件流转换为字节数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            byte[] fileBytes = outputStream.toByteArray();

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

            // 封装文件数据和响应头
            Map<String, Object> response = new HashMap<>();
            response.put("data", Base64.getEncoder().encodeToString(fileBytes)); // 使用Base64编码
            response.put("headers", headers);
            response.put("contentType", MediaType.APPLICATION_OCTET_STREAM);

            return response;
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }
}
